package haois.module;

import haois.constant.Reply;
import haois.security.HandleToken;
import haois.util.RespUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * Created by RuiHaoZ on 2017/11/20.
 */
@RestController
public class BaseController {

    private Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Value("${conf.upstream}")
    private String upstream;

    @Value("${image_upload_url}")
    public String imgDirFilePath;

    @Value("${excel.profiles.templet}")
    public String excelTemplet;

    @Value("${excel_upload_url}")
    public String excelDirFilePath;

    @Value("${system_admin_ids}")
    public String sysAdminIds;

    public HandleToken handleToken = new HandleToken();


    @RequestMapping(value = "/api", method = RequestMethod.GET)
    public String getApi() {
        List<Reply> replies = Reply.getApiMsg();
        Reply<List<Reply>> reply = new Reply<List<Reply>>("00", upstream, replies);
        return reply.toJson();
    }

    public boolean isSystem(String mid) {
        try {
            String[] systems = sysAdminIds.split(";");
            for (int i = 0; i < systems.length; i++) {
                if (systems[i].equals(mid)) {
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @RequestMapping(value = "/json")
    public String index1(HttpServletResponse response, String json) {
        logger.info(json);
        RespUtil.setACAO(response);
        return json;
    }

    @RequestMapping(value = "/json/{json}")
    public String index2(HttpServletResponse response,@PathVariable String json) {
        logger.info(json);
        RespUtil.setACAO(response);
        return json;

    }
}
